﻿Imports ComponentFactory.Krypton.Toolkit
Imports System.Data.OleDb

Public Class consultaUsuarioadm
    Inherits KryptonForm

#Region "var"
    Dim cmd As New OleDbCommand
    Dim ds As New DataSet
    Dim dataAdapter As OleDbDataAdapter
    Dim conex As OleDbConnection
    Dim strcarrega = "SELECT tblLogin.codLogin, tblLogin.usuarioLog, tblLogin.emailUsuario, tblNivelUsuario.descricaonivelUsuario " & _
                     "FROM tblNivelUsuario INNER JOIN tblLogin ON tblNivelUsuario.codnivelUsuario = tblLogin.codnivelUsuario WHERE codLogin > 1 AND codLogin <> {0}"
#End Region

    Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        conex = FabricaDeConexoes.CriarConexao()
        conex.Open()
        carregargrid()
    End Sub

    Private Sub carregargrid()

        cmd = New OleDbCommand(String.Format(strcarrega, Login.usuario.Codigo), conex)
        dataAdapter = New OleDbDataAdapter(cmd)
        ds = New DataSet()
        Try
            If (ds IsNot Nothing) Then
                dataAdapter.Fill(ds, "tblUsuario")
                dtgusuario.DataSource = ds.Tables("tblUsuario")
                dtgusuario.Columns(0).Visible = False
                dtgusuario.Columns(1).HeaderText = "Usuário"
                dtgusuario.Columns(2).HeaderText = "Email"
                dtgusuario.Columns(3).HeaderText = "Tipo"
            End If
        Catch ex As Exception
            Debug.WriteLine(ex)
        End Try
    End Sub
    Private Sub btnnovo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnovo.Click
        Dim frm As New CadastroUsuario()
        frm.ShowDialog()
        carregargrid()
    End Sub

    Private Sub excluirconta()
        Dim strexcluir = "DELETE * FROM tblLogin WHERE codLogin = " & dtgusuario.CurrentRow.Cells(0).Value

        Try
            cmd.CommandText = strexcluir
            cmd.ExecuteNonQuery()
            carregargrid()
        Catch ex As Exception
            Debug.WriteLine(ex)
        End Try
    End Sub

    Private Sub btnexcluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexcluir.Click
        If (KryptonMessageBox.Show("Deseja realmente excluir esta conta?", "Atenção", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
            excluirconta()
            carregargrid()
        End If
    End Sub
End Class